<!--验票、详情页面-->
<template>
  <div class="invoice-list-box">
    <el-container class="partDetails">
      <!-- tab搜索 -->
      <el-header
        height="auto"
        class="no-padding">
        <tabs
          is-new-tabs
          :tabs-arr="detailTabs"
          @returnIndex="tabsHandle" />
      </el-header>
      <div v-show="tabIndex === 1">
        <div
          class="remark common-ui"
          v-if="isShowRemark">
          <span style="color:red;">备注：</span>
          <span>{{ headerInfo.remark }}</span>
        </div>
        <invoice-view
          :invoice-no="invoiceNo"
          :disabled="isShowRemark"
        />
      </div>
      <template v-if="tabIndex === 2">
        <div class="product-info-box common-ui">
          <p>产品总数：<span>{{ headerInfo.prodTotalCount }}</span></p>
          <p>品种数：<span>{{ headerInfo.prodTypeCount }}</span></p>
          <p>金额合计（元）：<span style="color:red;">{{ getFormatterPrice(headerInfo.orderAmount) }}</span></p>
        </div>
        <el-editable
          class="distributor-edit-box"
          :data="invoiceList"
          height="400"
          v-loading="tableLoading"
          :columns="invoiceTableColumns"
        >
          <template
            slot="default-hosRegistNo"
            slot-scope="scope">
            <el-button
              :disabled="!scope.row.hosRegistNo||!scope.row.hosRegistId"
              v-html="regisNoButton(scope.row, ['hosRegistNo', 'hosRegistId'])"
              @click="showPdfRegistion(scope.row.hosRegistId)"
              type="text"
            />
          </template>
        </el-editable>
        <el-pagination
          align="right"
          style="margin-top:10px;"
          @size-change="sizeChange"
          @current-change="pageChange"
          :page-sizes="PAGE_NUMBER"
          :page-size="pageSize"
          :current-page="curPage"
          :layout="PAGINATION_LAYOUT"
          :total="totalElements"
        />
      </template>
      <!-- 验票拒绝弹窗 -->
      <el-dialog
        title="审核拒绝"
        :visible.sync="isShowReject"
        width="30%"
        :before-close="handleClose">
        <p>
          <span>备注</span>
          <el-input
            type="textarea"
            :rows="4"
            :maxlength="98"
            show-word-limit
            v-model="rejectRemark" />
        </p>
        <span
          slot="footer"
          class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
          <el-button
            type="primary"
            @click="confirm()">确 定</el-button>
        </span>
      </el-dialog>
    </el-container>
  </div>
</template>

<script>
import { statusArr, invoiceTableColumns, nissinReadColunmns, detailTabs } from './const'
import { PAGE_NUMBER, PAGINATION_LAYOUT } from '@const/common'
import { formatPrice, regisNoButton, showPdfRegistion } from '@const/agencySaas'
import RecordInfo from '@/moduleComponents/beforeSettle/distributorSide/purchasingManagement/record'
import {
  hospitalSettleVerifyManagerOrderDetail, // 医院-后结算-验票订单详情信息
  invoiceDistinguishForOCR, // 配送商/医院-后结算-ocr图片识别接口
  hospitalSettleVerifyManagerVerifyInvoice // 医院-后结算-验票操作
} from '@const/beforeSettleApi'
import ElEditable from '@components/el-editable'
import { cloneDeep, isObject, hasIn } from 'lodash'
import { getFormatterPrice, formatDate } from '@const/filter'
import tabs from '@components/tabs/index.vue'
import InvoiceView from '@/moduleComponents/common/ticket/invoice-veiw'
import UploadImg from '@/moduleComponents/beforeSettle/common/upload-img.vue'
export default {
  components: { ElEditable, RecordInfo, tabs, InvoiceView, UploadImg },
  data () {
    return {
      isShowReject: false,
      uploadDisabled: false,
      invoiceUrl: '',
      detailTabs,
      tabIndex: 1,
      id: this.$route.query.id,
      invoiceNo: this.$route.query.invoiceNo,
      // 表格Loading
      tableLoading: false,
      // 表单 详情展示内容-table
      invoiceList: [],
      invoiceTableColumns,
      // 订单详情信息
      headerInfo: {},
      nissinReadColunmns,

      curPage: 1, // 当前页
      PAGE_NUMBER, // 页码数组
      PAGINATION_LAYOUT, // 页码功能布局
      pageSize: PAGE_NUMBER[0], // 一页的数量
      totalElements: 0, // 总共的条数
      rejectRemark: ''
    }
  },
  computed: {
    isShowRemark () {
      return !![3, '3'].includes(this.$route.query.orderStatus)
    }
  },
  mounted () {
    this.getDataInfo()
    this.setBtn()
  },
  methods: {
    formatPrice,
    regisNoButton,
    showPdfRegistion,
    getFormatterPrice,
    getInvoiceData (val) {
      this.invoiceUrl = val.invoiceUrl
      this.remark = val.remark
    },
    setBtn () {
      let arr = [{ type: '', val: '返回', handle: () => this.$router.go(-1) }]
      // 如果是验票
      if (this.$route.query.type === 'ticketcheck') {
        arr = [
          { type: '', val: '返回', handle: () => this.$router.go(-1) },
          {
            type: 'danger',
            val: '验票拒绝',
            handle: this.handlenReject
          },
          { type: 'primary', val: '验票通过', handle: this.handlenPass }
        ]
      } else {
        arr = [{ type: '', val: '返回', handle: () => this.$router.go(-1) }]
      }
      this.$store.commit('changeLayoutHandle', {
        type: 'btn',
        arr: arr
      })
    },
    // 验票拒绝
    handlenReject () {
      this.isShowReject = true
    },
    cancel () {
      this.isShowReject = false
      this.rejectRemark = ''
    },
    // 验票通过
    handlenPass () {
      this.ticketcheckOperation('pass')
    },
    // 验票拒绝弹窗确定
    confirm () {
      this.ticketcheckOperation('reject')
      this.isShowReject = false
    },
    handleClose () {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.cancel()
        })
        .catch(_ => {})
    },
    // 验票操作
    ticketcheckOperation (val) {
      let operate
      if (val === 'pass') {
        operate = 2
      } else {
        operate = 3
      }
      this.$axios
        .$post(hospitalSettleVerifyManagerVerifyInvoice, {
          invoiceNo: this.invoiceNo,
          operate,
          remark: this.rejectRemark
        })
        .then(res => {
          if (res.payload) {
            if ((operate === 2)) {
              this.$message({ type: 'success', message: '验票通过' })
              this.$router.go(-1)
            } else {
              this.$message({ type: 'success', message: '验票拒绝' })
              this.rejectRemark = ''
              this.$router.go(-1)
            }
          }
        })
    },
    // tabs切换处理事件
    tabsHandle (index) {
      this.tabIndex = this.detailTabs[index].val || ''
    },
    // 获取列表数据
    getDataInfo () {
      let data = {
        orderId: this.id,
        invoiceNo: this.invoiceNo,
        page: this.curPage,
        pageSize: this.pageSize
      }
      this.tableLoading = true
      this.$axios({
        method: 'post',
        url: hospitalSettleVerifyManagerOrderDetail,
        data: data
      })
        .then(res => {
          let { invoiceProductList, ...headerInfo } = hasIn(res, 'data.payload') ? res.data.payload : {}
          this.headerInfo = headerInfo
          this.invoiceList = invoiceProductList.content || []
          this.totalElements = invoiceProductList.totalElements

          if (this.headerInfo.invoiceAmountTax) {
            this.headerInfo.invoiceAmountTax = getFormatterPrice(this.headerInfo.invoiceAmountTax)
          }

          // 如果有空数据，处理一下，变成“--”
          let objKey = Object.keys(this.headerInfo)
          objKey.forEach(itemKey => {
            if (!this.headerInfo[itemKey]) {
              this.headerInfo[itemKey] = '--'
            }
          })
        })
        .finally(() => {
          this.tableLoading = false
        })
    },
    // 页码更改
    sizeChange (size) {
      this.pageSize = size
      this.curPage = 1
      this.getDataInfo()
    },

    // 切换不一样的页面,重置一切数据
    pageChange (curPage) {
      this.curPage = curPage
      this.getDataInfo()
    }
  }
}
</script>
<style lang="less" scoped>
@import '../m-front-common/pc/assets/var.less';
.invoice-list-box {
  background: @bgColor;
  height: 100%;
  .no-padding.el-header {
    padding: 0;
  }
  .common-ui {
    padding: 8px 0;
    background: #fff;
    margin-bottom: 8px;
  }
  .partDetails {
    height: 100%;
    .product-info-box {
      display: flex;
      padding: 8px;
      p {
        margin-right: 20px;
        span {
          color: blue;
        }
      }
    }
  }
  .remark {
    border: 1px solid #dfe2e8;
    padding: 8px;
    font-size: 16px;
  }

  .el-dialog {
    p {
      span {
        display: inline-block;
        width: 48px;
      }

      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
